
/**
 * created on 2012-04-28
 * @author  qiujz
 * @version 1.0
 *
 * Copyright (c)  Founder Software (Suzhou) Co., Ltd.  2012
 *  All rights reserved.
 *
 * 
 * ================================================================
 * Change Revision
 * ---------------------------------------------------------------
 *    DATE                BY                   COMMENT
 * ---------------------------------------------------------------
 * 2012-04-28         qiujz                Create class
 * 
 *
 * ================================================================
 */

package com.qjz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.founder.mh.framework.entity.QueryResultBase;
import com.founder.mh.framework.util.BuildTableUtil;
import com.qjz.dao.IUserInfoDao;
import com.qjz.entity.UserInfoQueryParameter;
import com.qjz.entity.UserInfoTo;
import com.qjz.service.IUserInfoService;
import com.founder.mh.framework.entity.Sort;
import com.founder.mh.framework.service.impl.BaseServiceImpl;

@WebService(endpointInterface = "com.qjz.service.IUserInfoService")
public class UserInfoServiceImpl extends BaseServiceImpl implements IUserInfoService {

	private static final Log LOGGER = LogFactory.getLog(UserInfoServiceImpl.class);

	private IUserInfoDao userInfoDao;
	
	/**
	 * @return Dao接口
	 */
	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	
	/**
	 * @param userInfoDao
	 *            Dao接口
	 */
	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IUserInfoService
	 *      #searchUserInfoList(com.qjz.entity.UserInfoQueryParameter)
	 */
	public QueryResultBase searchUserInfoList(UserInfoQueryParameter paramter) {
		if (paramter == null) {
			throw new RuntimeException("查询参数对象不能为空!");
		}
		
		// 放置查询结果的对象
		QueryResultBase rtnResult = new QueryResultBase();

		// 设置查询参数
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// ToDO Add parameter
		if(null != paramter.getUserInfo()){
			paramMap.put(BuildTableUtil
							.buildPropertyNameFromColumn(UserInfoTo.XING_MING), paramter.getUserInfo().getXingMing());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(UserInfoTo.YONG_HU_MING), paramter.getUserInfo().getYongHuMing());
			paramMap.put(BuildTableUtil
					.buildPropertyNameFromColumn(UserInfoTo.MI_MA), paramter.getUserInfo().getMiMa());
		}
		List<UserInfoTo> userInfoList = null;
		int total = userInfoDao.getUserInfoCount(paramMap);
		rtnResult.setTotal(total);
		if (total == 0) {
			return rtnResult;
		}
		
		// 设置排序字段
		List<Sort> sortList = paramter.getSort();
		if (sortList != null && sortList.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (Sort sort : sortList) {
				sb.append(sort.getColumnName()).append(" ").append(
						sort.getOrder()).append(",");
			}

			paramMap.put("sort", sb.toString()
					.substring(0, sb.lastIndexOf(",")));

			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug(paramMap.get("sort"));
			}
		}
		
		// 不分页的情况
		if (paramter.getStartRow() <= 0) {
			userInfoList= userInfoDao.searchUserInfoList(paramMap);
		} else {
			// 执行分页询
			int endRow = paramter.getStartRow() + paramter.getLimit() - 1;
			userInfoList = userInfoDao.searchUserInfoPagination(paramMap,
					paramter.getStartRow(), endRow);
		}
		rtnResult.setPageData(userInfoList);
		return rtnResult;
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IUserInfoService
	 *      #getUserInfoByPrimaryKey(int)
	 */
	public UserInfoTo getUserInfoByPrimaryKey(int uid) {
		return userInfoDao.getUserInfoByPrimaryKey(uid);
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IUserInfoService
	 *      #insertUserInfo(com.qjz.entityUserInfo.UserInfoTo)
	 */
	public UserInfoTo insertUserInfo(UserInfoTo userInfo) {
		
			 userInfoDao.insertUserInfo(userInfo);
			 //userInfo = userInfoDao.getUserInfoByPrimaryKey(uid);
		
		
		return userInfo;
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IUserInfoService
	 *      #updateUserInfo(com.qjz.entity.UserInfoTo)
	 */
	public int updateUserInfo(UserInfoTo userInfo) {
		return userInfoDao.updateUserInfoByBean(userInfo);		
	}
	
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IUserInfoService
	 *      #deleteUserInfo(int)
	 */
	public int deleteUserInfo(int uid) {
		return userInfoDao.deleteUserInfoByPrimaryKey(uid);
	}
	
	/** 
	 * @inheritDoc
	 * @see com.qjz.service.IUserInfoService
	 *      #deleteUserInfoBatch(int[])
	 */
	public void deleteUserInfoBatch(int[] keyArray) {
		userInfoDao.deleteUserInfoBatch(keyArray);
	}
	
}
	
